/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gifviewer;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Luke Atkins
 */
public class ViewFrame extends JFrame {

   private String path;
   private String url;
   private Icon imageIcon;
   private JLabel imageLabel;
   private JPanel buttons;
   private JButton forward;
   private JButton backward;
   private JButton open;
   private ArrayList<String> fileList;

   ViewFrame(String path) {
      this.path = path.substring(0, path.lastIndexOf('\\') + 1);
      this.url = path.substring(path.lastIndexOf('\\') + 1);
      //System.out.println("Path: [" + this.path + "], File: [" + url + "]");
      fileList = getFileList(this.path);
      imageIcon = new ImageIcon(path);
      imageLabel = new JLabel(imageIcon);

      getContentPane().add(imageLabel);

      setupButtons();
      addListeners();
      addKeyListener(new GIFViewKeys("ViewFrame"));

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(500, 500);
      setLocationRelativeTo(null);
      setVisible(true);
   }

   ViewFrame() {
      this("C:\\GIFViewer\\Images\\image.gif");
   }

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {
      // TODO code application logic here
      System.out.println("Starting GIFViewer");
      ViewFrame viewer;
      if (args.length == 0) {
         viewer = new ViewFrame();
      } else {
         viewer = new ViewFrame(args[0]);
      }
   }

   public void setImage(String path, String url) {
      this.path = path;
      this.url = url;
      imageIcon = new ImageIcon(path + url);
      imageLabel.setIcon(imageIcon);
      setTitle(url);
      fileList = getFileList(this.path);
      if (getFileList(this.path).size() < 2) {
         backward.setEnabled(false);
         forward.setEnabled(false);
      }
   }

   private void setupButtons() {
      buttons = new JPanel();
      forward = new JButton("next");
      backward = new JButton("back");
      open = new JButton("open");
      buttons.add(backward);
      buttons.add(forward);
      buttons.addKeyListener(new GIFViewKeys("buttons"));
      forward.addKeyListener(new GIFViewKeys("forward"));
      backward.addKeyListener(new GIFViewKeys("backward"));
      open.addKeyListener(new GIFViewKeys("open"));
      imageLabel.addKeyListener(new GIFViewKeys("imageLabel"));
      if (getFileList(path).size() < 2) {
         backward.setEnabled(false);
         forward.setEnabled(false);
      }
      getContentPane().add(BorderLayout.NORTH, open);
      getContentPane().add(BorderLayout.SOUTH, buttons);
   }

   private void addListeners() {
      forward.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            int index = fileList.indexOf(url);
            //System.out.println(index);
            int nextImage;
            if (index == fileList.size() - 1) {
               nextImage = 0;
            } else {
               nextImage = index + 1;
            }
            setImage(path, fileList.get(nextImage));
         }
      });

      backward.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            int index = fileList.indexOf(url);
            int nextImage;
            if (index == 0) {
               nextImage = fileList.size() - 1;
            } else {
               nextImage = index - 1;
            }
            setImage(path, fileList.get(nextImage));
         }
      });

      open.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            openFile();
         }
      });
   }

   private ArrayList<String> getFileList(String dirUrl) {
      //System.out.println("dirUrl: " + dirUrl);
      File dir = new File(dirUrl);
      ArrayList<String> children = new ArrayList<String>();
      String[] files = dir.list();
      if (files == null) {
         //System.out.println("DIR IS NULL");
      }
      for (int i = 0; i < dir.list().length; ++i) {
         if (files[i].contains(".gif")) {
            children.add(files[i]);
         }
      }
      String[] imageList = new String[children.size()];
      for (int i = 0; i < children.size(); ++i) {
         imageList[i] = children.get(i);
      }
      return children;
   }

   private void openFile() {
      FileInputStream fileStream;
      ObjectInputStream os;

      JFrame openFrame = new JFrame("Music Mixer - Open File");
      openFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

      JFileChooser openWindow = new JFileChooser();
      JTextField filename = new JTextField();
      JTextField dir = new JTextField();
      boolean fileChosen = true;

      openWindow.setCurrentDirectory(new File(path));
      openWindow.setFileFilter(new FileNameExtensionFilter("GIF, JPG, JPEG, PNG", "gif", "jpg", "jpeg", "png"));

      int rVal = openWindow.showOpenDialog(openFrame);
      if (rVal == JFileChooser.APPROVE_OPTION) {
         filename.setText(openWindow.getSelectedFile().getName());
         dir.setText(openWindow.getCurrentDirectory().toString());
      }
      if (rVal == JFileChooser.CANCEL_OPTION) {
         filename.setText("You pressed cancel");
         dir.setText("");
         fileChosen = false;
      }

      if (fileChosen) {
         path = dir.getText() + "\\";
         url = filename.getText();
         setImage(path, url);
      }
   }

   public class GIFViewKeys implements KeyListener {

      String componentName;

      GIFViewKeys(String str) {
         super();
         componentName = str;
      }

      public void keyTyped(KeyEvent e) {
         //System.out.println("KeyTyped by " + componentName);
      }

      public void keyPressed(KeyEvent e) {
         //System.out.println("KeyPressed by " + componentName);
      }

      public void keyReleased(KeyEvent e) {
         //System.out.println("KeyReleased by " + componentName);
         int index;
         int nextImage;
         switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
               index = fileList.indexOf(url);
               if (index == 0) {
                  nextImage = fileList.size() - 1;
               } else {
                  nextImage = index - 1;
               }
               setImage(path, fileList.get(nextImage));
               break;
            case KeyEvent.VK_RIGHT:
               index = fileList.indexOf(url);
               if (index == fileList.size() - 1) {
                  nextImage = 0;
               } else {
                  nextImage = index + 1;
               }
               setImage(path, fileList.get(nextImage));
               break;
         }
      }
   }
}
